﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Data;
using System.Text;
using ElingOA6_Admin_DSJCJ.Areas.Admin.Models;
using Newtonsoft.Json;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 用品部门统计
    /// </summary>
    /// 
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class OfficeItemDeptCountController : AdminBaseController
    {
        //
        // GET: /Admin/OfficeItemDeptCount/
        /// <summary>
        /// 用品部门统计首页
        /// </summary>
        /// <returns></returns>
        /// 
        [AuthorityActionFilter("m_OfficeItemDeptCount", Order = 1)]
        [LogPageActionFilter("用品部门统计", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult OfficeItemDeptCountList(string deptNo = "", string startDate = "", string endDate = "")
        {
            BLL.OfficeItemDeptCount deptCountBll = new BLL.OfficeItemDeptCount();

            List<Model.OfficeItemDeptCount> deptCountList = deptCountBll.GetAllList(deptNo, startDate, endDate);

            decimal zqty = 0;
            decimal zje = 0;
            StringBuilder strHtml = new StringBuilder();
            string strDeptNo = "";

            decimal xqty = 0;
            decimal xje = 0;
            decimal count = 0;
            decimal count1 = 0;
            strHtml.Append("<table width='96%' class='tableForm'><tr>");
            strHtml.Append("<td class='tdClass' align='center' height='25'><font size='4px'>部门</font></td>");
            strHtml.Append("<td class='tdClass' align='center'><font size='4px'>物品编号</font></td>");
            strHtml.Append("<td class='tdClass' align='center'><font size='4px'>名称</font></td>");
            strHtml.Append("<td class='tdClass' align='center'><font size='4px'>规格</font></td>");
            strHtml.Append("<td class='tdClass' align='center'><font size='4px'>领用数量</font></td>");
            strHtml.Append("<td class='tdClass' align='center'><font size='4px'>金额</font></td></tr>");
            foreach (Model.OfficeItemDeptCount model in deptCountList)
            {
                count1++;       
                if (strDeptNo != model.FApplyDeptNo)
                {
                    count = 0;
                    xqty = 0;
                    xje = 0;
                    strDeptNo = model.FApplyDeptNo;
                    strHtml.Append("<tr><td class='tdClass' align='center' height='25'><font size='3px'><a href='#' onclick='popDialog(\"" + model.FApplyDeptNo + "\")'>"
                        + model.FDeptName + "</a></font></td><td class='tdClass' align='center'><font size='3px'>"
                        + model.FItemNo + "</font></td><td class='tdClass' align='center'><font size='3px'>"
                        + model.FItemName + "</font></td><td class='tdClass' align='center'><font size='3px'>"
                        + model.FItemSpec + "</font></td><td class='tdClass'align='right'><font size='3px'>"
                        + model.fqy + "</font></td><td class='tdClass' align='right'><font size='3px'>" + model.amount + "</font></td></tr>");
                    count++;
                    xqty += model.fqy;
                    xje += model.amount;
                }
                else 
                {
                    strHtml.Append("<tr><td class='tdClass' align='center' height='25'></td><td class='tdClass' align='center'><font size='3px'>"
                        + model.FItemNo + "</font></td><td class='tdClass' align='center'><font size='3px'>"
                        + model.FItemName + "</font></td><td class='tdClass' align='center'><font size='3px'>"
                        + model.FItemSpec + "</font></td><td class='tdClass' align='right'><font size='3px'>"
                        + model.fqy + "</font></td><td class='tdClass' align='right'><font size='3px'>" + model.amount + "</td></tr>");
                    count++;
                    xqty += model.fqy;
                    xje += model.amount;
                }

                var query = deptCountList.Where(c => c.FApplyDeptNo == strDeptNo).ToList();
                if (count == query.Count())
                {
                    strHtml.Append("<tr><td class='tdClass' align='center' height='25'><font size='4px'>小计</font></td><td class='tdClass'></td><td class='tdClass'></td><td class='tdClass'></td><td class='tdClass' align='right'><font size='4px'>"
                           + xqty + "</font></td><td class='tdClass' align='right'><font size='4px'>" + xje + "</font></td></tr>");
                }

                zqty += model.fqy;
                zje += model.amount;

                if (count1 == deptCountList.Count())
                {
                    strHtml.Append("<tr><td class='tdClass' align='center' height='25'><font size='4px'>合计</font></td><td class='tdClass'></td><td class='tdClass'></td><td class='tdClass'></td><td class='tdClass' align='right'><font size='4px'>"
                       + zqty + "</font></td><td class='tdClass' align='right'><font size='4px'>" + zje + "</font></td></tr>");                                       
                }
            }
            strHtml.Append("</table>");
            ViewData["strHtml"] = strHtml;

            ///获取部门
            string entityNo = GlobalConstant.ENTITYNO;
            BLL.HRDept deptBll = new BLL.HRDept();
            List<Model.HRDept> deptList = deptBll.GetListBYFIsAgencyProp(entityNo, "");

            List<SelectListItem> listItem = new List<SelectListItem>();
            SelectListItem select = new SelectListItem();
            select.Text = "全部";
            select.Value = "";
            listItem.Add(select);
            ViewData["deptNo"] = "";
            foreach (Model.HRDept model in deptList) 
            {
                select = new SelectListItem();
                select.Text = model.FDeptName;
                select.Value = model.FDeptNo;
                listItem.Add(select);
                
            }
            ViewData["deptList"] = listItem;


            ViewData["deptNo"] = deptNo;
            ViewData["startDate"] = startDate;
            ViewData["endDate"] = endDate;

            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="deptNo"></param>
        /// <returns></returns>
        public ActionResult OfficeItemDeptCountInfo(string deptNo)
        {

            BLL.OfficeItemDeptCount deptCountBll = new BLL.OfficeItemDeptCount();
            List<Model.OfficeItemDeptCount> deptCountList = deptCountBll.GetAllList1(deptNo, "", "");

            ///把数据在页面上显示
            ///
            decimal zqty = 0;
            decimal zamount = 0;
            StringBuilder strHtml = new StringBuilder();
            foreach (Model.OfficeItemDeptCount model in deptCountList)
            {
                string date = model.FAuditDate.ToString("yyyy-MM-dd");
                if (date == "0001-01-01") {
                    date = "";
                }

                strHtml.Append("<tr height='30'>");
                strHtml.Append("<td class='tdClass' align='center'>" + model.FDeptName + "</td>");
                strHtml.Append("<td class='tdClass' align='center'>" + model.FEmpName + "</td>");
                strHtml.Append("<td class='tdClass' align='center'><a href='#' onclick='popDialog(\"" + model.FItemRequisitionApplyID + "\")'>" + model.FItemRequisitionApplyID + "</a></td>");
                strHtml.Append("<td class='tdClass' align='center'>" + date + "</td>");
                strHtml.Append("<td class='tdClass' align='center'>" + model.FItemName + "</td>");
                strHtml.Append("<td class='tdClass' align='center'>" + model.FItemSpec + "</td>");
                strHtml.Append("<td class='tdClass' align='right'>" + model.FQty + "</td>");
                strHtml.Append("<td class='tdClass' align='right'>" + model.FPrice + "</td>");
                strHtml.Append("<td class='tdClass' align='right'>" + model.FAmount + "</td>");
                strHtml.Append("</tr>");
                zqty += model.FQty;
                zamount += model.FAmount;
            }

            strHtml.Append("<tr height='30'><td class='tdClass' align='center'>合计</td><td class='tdClass' align='center'>");
            strHtml.Append("</td><td class='tdClass' align='center'></td><td class='tdClass' align='center'></td><td class='tdClass' align='center'></td><td class='tdClass' align='center'></td>");
            strHtml.Append("<td class='tdClass' align='right'>" + zqty + "</td><td class='tdClass' align='center'></td>");
            strHtml.Append(" <td class='tdClass' align='right'>" + zamount + "</td></tr>");

            ViewData["strHtml"] = strHtml;

            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="itemRequisitionApplyID"></param>
        /// <returns></returns>
        public ActionResult OfficeItemDeptCountShowUse(string itemRequisitionApplyID)
        {
            BLL.ItemRequisitionApply itemRequisitionApplyBLL = new BLL.ItemRequisitionApply();
            Model.ItemRequisitionApply itemRequisitionApplyModel = new Model.ItemRequisitionApply();

            int z = 0;

            itemRequisitionApplyModel = itemRequisitionApplyBLL.GetModel(int.Parse(itemRequisitionApplyID));

            BLL.ItemRequisitionApplyEntry itemRequisitionApplyEntryBLL = new BLL.ItemRequisitionApplyEntry();
            List<Model.ItemRequisitionApplyEntry> itemRequisitionApplyEntryList = itemRequisitionApplyEntryBLL.GetModelList("FItemRequisitionApplyID=" + itemRequisitionApplyID + "");

            BLL.HREmpl HREmplBLL = new BLL.HREmpl();
            string str = HREmplBLL.GetEmpNameAndDept(Common.GlobalConstant.ENTITYNO, itemRequisitionApplyModel.FApplyEmplNo);

            SelectEmpModel empModel = JsonConvert.DeserializeObject<SelectEmpModel>(str);

            itemRequisitionApplyModel.FApplyEmplName = empModel.FEmpName;
            itemRequisitionApplyModel.FApplyDeptName = empModel.FDeptName;

            ViewData["ShowList"] = GetTable(GetShowList(itemRequisitionApplyEntryList), ref z);

            return View(itemRequisitionApplyModel);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ItemRequisitionApplyEntryList"></param>
        /// <param name="z"></param>
        /// <returns></returns>
        public string GetTable(List<Model.ItemRequisitionApplyEntry> ItemRequisitionApplyEntryList, ref int z)
        {
            StringBuilder str = new StringBuilder();
            foreach (Model.ItemRequisitionApplyEntry model in ItemRequisitionApplyEntryList)
            {
                z++;
                str.Append("<tr id='" + z + "'>");
                str.Append(" <td align='center' class='tdClass'>" + model.FItemName + "</td>");
                str.Append(" <td align='center' class='tdClass'>" + model.FPrice + "</td>");
                str.Append(" <td align='center' class='tdClass'>" + model.FQcQty + "</td>");
                str.Append(" <td align='center' class='tdClass'>" + model.FApplyQty + "</td>");
                //str.Append(" <td align='center' class='tdClass'><input type='text' id='FPurchaseDate' name='FPurchaseDate' value=''></td></tr>");
            }
            return str.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<Model.ItemRequisitionApplyEntry> GetShowList(List<Model.ItemRequisitionApplyEntry> list)
        {
            if (list.Count > 0)
            {
                string nolist = "";
                foreach (Model.ItemRequisitionApplyEntry model in list)
                {
                    if (nolist != "")
                    {
                        nolist += "," + model.FItemNo;
                    }
                    else
                    {
                        nolist += model.FItemNo;
                    }
                }

                BLL.Item ItemBLL = new BLL.Item();
                List<Model.Item> ItemList = ItemBLL.GetListAll("FItemNo in (" + nolist + ")");

                foreach (Model.ItemRequisitionApplyEntry model in list)
                {
                    List<Model.Item> selectItemList = (from nm in ItemList where nm.FItemNo == model.FItemNo select nm).ToList();
                    model.FItemName = selectItemList[0].FItemName;
                    model.FPrice = selectItemList[0].FPrice;
                    model.FQcQty = selectItemList[0].FQty.ToString();
                }
            }
            return list;
        }

        int rowIndex = 1;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="deptNo"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        public void OfficeItemDeptCountExportToExcel(string deptNo, string startDate, string endDate)
        {           
            List<Model.XlsModel> xlsModels = new List<Model.XlsModel>();

            Model.XlsModel xlsModel = new Model.XlsModel();

            #region 静态数据
            //第一行
            xlsModel = new Model.XlsModel();
            xlsModel.RowIndex = 1;
            xlsModel.Title = "部门";
            xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xlsModels.Add(xlsModel);

            xlsModel = new Model.XlsModel();
            xlsModel.RowIndex = 1;
            xlsModel.Title = "物品编号";
            xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xlsModels.Add(xlsModel);

            xlsModel = new Model.XlsModel();
            xlsModel.RowIndex = 1;
            xlsModel.Title = "名称";
            xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xlsModels.Add(xlsModel);

            xlsModel = new Model.XlsModel();
            xlsModel.RowIndex = 1;
            xlsModel.Title = "规格";
            xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xlsModels.Add(xlsModel);

            xlsModel = new Model.XlsModel();
            xlsModel.RowIndex = 1;
            xlsModel.Title = "领用数量";
            xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xlsModels.Add(xlsModel);

            xlsModel = new Model.XlsModel();
            xlsModel.RowIndex = 1;
            xlsModel.Title = "金额";
            xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
            xlsModels.Add(xlsModel);

            #endregion


            List<Model.XlsModel> xlsModels1 = createXlsTable(deptNo, startDate, endDate);
            if (xlsModels1 != null)
            {
                xlsModels.AddRange(xlsModels1);
            }

            List<Model.XlsModel_Column> xlsModelColumns = new List<Model.XlsModel_Column>();
            Model.XlsModel_Column xmc = new Model.XlsModel_Column(); 
            xmc.ColumnIndexEnd = 1;
            xmc.ColumnIndexStart = 0; 
            xmc.Width = 25; 
            xlsModelColumns.Add(xmc);

            xmc = new Model.XlsModel_Column();
            xmc.ColumnIndexEnd = 5;
            xmc.ColumnIndexStart = 2; 
            xmc.Width = 15; 
            xlsModelColumns.Add(xmc);
            GenerateUtil.ExportToExcelReturnPath(".cls", "部门统计", rowIndex, xlsModels, xlsModelColumns, new List<Model.XlsModel_Row>());

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="deptNo"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        private List<Model.XlsModel> createXlsTable(string deptNo, string startDate, string endDate)
        {
            

            List<Model.XlsModel> xlsModels = new List<Model.XlsModel>();

            BLL.OfficeItemDeptCount deptCountBll = new BLL.OfficeItemDeptCount();

            DataSet ds = deptCountBll.GetAll(deptNo, startDate, endDate);

            List<Model.OfficeItemDeptCount> deptCountList = deptCountBll.DataTableToListOfficeItemDeptCount(ds.Tables[0]);

            string strDeptNo = "";

            decimal count = 0;
            decimal count1 = 0;

            decimal zqty = 0;
            decimal zje = 0;

            decimal xqty = 0;
            decimal xje = 0;


            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    rowIndex++;
                    count1++;    
                    if (strDeptNo != dr["FApplyDeptNo"].ToString())
                    {
                        count = 0;
                        xqty = 0;
                        xje = 0;

                        strDeptNo = dr["FApplyDeptNo"].ToString();
                        count++;
                     
                        Model.XlsModel xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = dr["FDeptName"].ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = dr["FItemNo"].ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = dr["FItemName"].ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = dr["FItemSpec"].ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = dr["fqy"].ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = dr["amount"].ToString() == "" ? "0" : dr["amount"].ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xqty += decimal.Parse(dr["fqy"].ToString() == "" ? "0" : dr["fqy"].ToString());
                        xje += decimal.Parse(dr["amount"].ToString() == "" ? "0" : dr["amount"].ToString());
                    }
                    else
                    {
                        count++;

                        Model.XlsModel xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = "";
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = dr["FItemNo"].ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = dr["FItemName"].ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = dr["FItemSpec"].ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = dr["fqy"].ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = dr["amount"].ToString() == "" ? "0" : dr["amount"].ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xqty += decimal.Parse(dr["fqy"].ToString() == "" ? "0" : dr["fqy"].ToString());
                        xje += decimal.Parse(dr["amount"].ToString() == "" ? "0" : dr["amount"].ToString());
                    
                    }

                    var query = deptCountList.Where(c => c.FApplyDeptNo == strDeptNo).ToList();
                    if (count == query.Count())
                    {
                        rowIndex++;
                        Model.XlsModel xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = "小计";
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = "";
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = "";
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = "";
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = xqty.ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = xje.ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                    }
                    zqty += decimal.Parse(dr["fqy"].ToString() == "" ? "0" : dr["fqy"].ToString());
                    zje += decimal.Parse(dr["amount"].ToString() == "" ? "0" : dr["amount"].ToString());

                    if (count1 == deptCountList.Count())
                    {
                        rowIndex++;
                        Model.XlsModel xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = "合计";
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = "";
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = "";
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = "";
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = zqty.ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                        xlsModel = new Model.XlsModel();
                        xlsModel.RowIndex = rowIndex;
                        xlsModel.Title = zje.ToString();
                        xlsModel.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xlsModels.Add(xlsModel);

                    }



                }
            }
            return xlsModels;
        
        }

    }
}
